package cn.kgc.kgcwxlogin.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author geQiang
 * @version 1.0
 * @date 2019/4/15
 * @description cn.kgc.kgcwxlogin.util
 */
@Component
public class AuthUtil {
    public static final String APPID = "wx9168f76f000a0d4c";
    public static final String APPSECRET = "8ba69d5639242c3bd3a69dffe84336c1";
    public static final String REDIRECT_URI = "http://localhost:8888/wxloginCallback";
    public static final String SCOPE = "snsapi_login";

    /**
     * send http request and convert resposne into json object
     * @param url
     * @return
     * @throws IOException
     */
    public static JSONObject doGetJson(String url) throws Exception {
        JSONObject jsonObject = null;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        //发送请求
        HttpResponse response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String result = EntityUtils.toString(entity, "UTF-8");
            jsonObject = JSONObject.parseObject(result);
        }
        //释放连接
        httpGet.releaseConnection();
        return jsonObject;
    }
}
